import 'dart:convert';
import 'package:cbec_app/entity/home/content/content_data.dart';

class GoodsDataModel extends ContentData {
  final int? pageMargin;
  final String? sizeType;
  final String? showOriginPrice;
  final String? displayScale;
  final String? imageFillStyle;
  final List<Goods>? goods;
  final String? title;
  final String? cornerMarkType; // Made optional
  final String? borderRadiusType; // Made optional
  final String? textAlignType;
  final int? goodsMargin;
  final String? showCornerMark;
  final String? uuid;
  final String? showSoldNum;
  final int? goodsNumberV2;
  final Map<String, dynamic>? goodsList;
  final String? size;
  final String? textStyleType;
  final String? price;
  final String? cornerMarkImage; // Made optional

  GoodsDataModel({
    this.pageMargin,
    this.sizeType,
    this.showOriginPrice,
    this.displayScale,
    this.imageFillStyle,
    this.goods,
    this.title,
    this.cornerMarkType,
    this.borderRadiusType,
    this.textAlignType,
    this.goodsMargin,
    this.showCornerMark,
    this.uuid,
    this.showSoldNum,
    this.goodsNumberV2,
    this.goodsList,
    this.size,
    this.textStyleType,
    this.price,
    this.cornerMarkImage,
  }) {
    type = 'goods';
  }

  factory GoodsDataModel.fromJson(Map<String, dynamic> json) {
    return GoodsDataModel(
      pageMargin: json['page_margin'],
      sizeType: json['size_type'],
      showOriginPrice: json['show_origin_price'],
      displayScale: json['display_scale'],
      imageFillStyle: json['image_fill_style'],
      goods: (json['goods'] as List?)?.map((item) => Goods.fromJson(item)).toList(),
      title: json['title'],
      cornerMarkType: json['corner_mark_type'], // Keep as is
      borderRadiusType: json['border_radius_type'], // Keep as is
      textAlignType: json['text_align_type'],
      goodsMargin: json['goods_margin'],
      showCornerMark: json['show_corner_mark'],
      uuid: json['uuid'],
      showSoldNum: json['show_sold_num'],
      goodsNumberV2: json['goods_number_v2'],
      goodsList: json['goods_list'],
      size: json['size'],
      textStyleType: json['text_style_type'],
      price: json['price'],
      cornerMarkImage: json['corner_mark_image'], // Keep as is
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'page_margin': pageMargin,
      'size_type': sizeType,
      'show_origin_price': showOriginPrice,
      'display_scale': displayScale,
      'image_fill_style': imageFillStyle,
      'goods': goods?.map((item) => item.toJson()).toList(),
      'type': type,
      'title': title,
      'corner_mark_type': cornerMarkType, // Keep as is
      'border_radius_type': borderRadiusType, // Keep as is
      'text_align_type': textAlignType,
      'goods_margin': goodsMargin,
      'show_corner_mark': showCornerMark,
      'uuid': uuid,
      'show_sold_num': showSoldNum,
      'goods_number_v2': goodsNumberV2,
      'goods_list': goodsList,
      'size': size,
      'text_style_type': textStyleType,
      'price': price,
      'corner_mark_image': cornerMarkImage, // Keep as is
    };
  }

  @override
  ContentData fromJson(Map<String, dynamic> json) {
    return GoodsDataModel.fromJson(json);
  }
}

class Goods {
  final int? commoditySource;
  final String? className;
  final double? priceMarket;
  final int? baseSpec;
  final int? number;
  final int? classId;
  final int? commodityStatus;
  final double? price;
  final double? highPrice;
  final String? id;
  final String? supplierName;
  final List<String>? image; // Made optional
  final String? brandName;
  final dynamic paySingle;
  final int? priceType;
  final List<Spec>? specList;
  final String? commodityId;
  final dynamic sort;
  final int? commodityType;
  final String? barCode;
  final String? spuSpecJsonBody;
  final double? taxRate;
  final String? mainImage;
  final double? priceCost;
  final List<String>? discountLabel;
  final int? commodityForm;
  final int? brandId;
  final double? highTaxRate;
  final String? skuCode;
  final String? commodityName;
  final int? status;

  Goods({
    this.commoditySource,
    this.className,
    this.priceMarket,
    this.baseSpec,
    this.number,
    this.classId,
    this.commodityStatus,
    this.price,
    this.highPrice,
    this.id,
    this.supplierName,
    this.image,
    this.brandName,
    this.paySingle,
    this.priceType,
    this.specList,
    this.commodityId,
    this.sort,
    this.commodityType,
    this.barCode,
    this.spuSpecJsonBody,
    this.taxRate,
    this.mainImage,
    this.priceCost,
    this.discountLabel,
    this.commodityForm,
    this.brandId,
    this.highTaxRate,
    this.skuCode,
    this.commodityName,
    this.status,
  });

  factory Goods.fromJson(Map<String, dynamic> json) {
    return Goods(
      commoditySource: json['commoditySource'],
      className: json['className'],
      priceMarket: json['priceMarket']?.toDouble(),
      baseSpec: json['baseSpec'],
      number: json['number'],
      classId: json['classId'],
      commodityStatus: json['commodityStatus'],
      price: json['price']?.toDouble(),
      highPrice: json['highPrice']?.toDouble(),
      id: json['id'],
      supplierName: json['supplierName'],
      image: (json['image'] != null) ? List<String>.from(jsonDecode(json['image'])) : null, // 解析字符串
      brandName: json['brandName'],
      paySingle: json['paySingle'],
      priceType: json['priceType'],
      specList: (json['specList'] as List?)?.map((item) => Spec.fromJson(item)).toList(),
      commodityId: json['commodityId'],
      sort: json['sort'],
      commodityType: json['commodityType'],
      barCode: json['barCode'],
      spuSpecJsonBody: json['spuSpecJsonBody'],
      taxRate: json['taxRate']?.toDouble(),
      mainImage: json['mainImage'],
      priceCost: json['priceCost']?.toDouble(),
      discountLabel: (json['discountLabel'] != null) ? List<String>.from(json['discountLabel']) : null,
      commodityForm: json['commodityForm'],
      brandId: json['brandId'],
      highTaxRate: json['highTaxRate']?.toDouble(),
      skuCode: json['skuCode'],
      commodityName: json['commodityName'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'commoditySource': commoditySource,
      'className': className,
      'priceMarket': priceMarket,
      'baseSpec': baseSpec,
      'number': number,
      'classId': classId,
      'commodityStatus': commodityStatus,
      'price': price,
      'highPrice': highPrice,
      'id': id,
      'supplierName': supplierName,
      'image': image, // Keep as is
      'brandName': brandName,
      'paySingle': paySingle,
      'priceType': priceType,
      'specList': specList?.map((item) => item.toJson()).toList(),
      'commodityId': commodityId,
      'sort': sort,
      'commodityType': commodityType,
      'barCode': barCode,
      'spuSpecJsonBody': spuSpecJsonBody,
      'taxRate': taxRate,
      'mainImage': mainImage,
      'priceCost': priceCost,
      'discountLabel': discountLabel,
      'commodityForm': commodityForm,
      'brandId': brandId,
      'highTaxRate': highTaxRate,
      'skuCode': skuCode,
      'commodityName': commodityName,
      'status': status,
    };
  }
}

class Spec {
  final dynamic taxRate;
  final double? priceCost;
  final int? specNum;
  final double? price;
  final double? highPrice;
  final double? priceMarket;
  final double? profit;

  Spec({
    this.taxRate,
    this.priceCost,
    this.specNum,
    this.price,
    this.highPrice,
    this.priceMarket,
    this.profit,
  });

  factory Spec.fromJson(Map<String, dynamic> json) {
    return Spec(
      taxRate: json['taxRate'],
      priceCost: json['priceCost']?.toDouble(),
      specNum: json['specNum'],
      price: json['price']?.toDouble(),
      highPrice: json['highPrice']?.toDouble(),
      priceMarket: json['priceMarket']?.toDouble(),
      profit: json['profit']?.toDouble(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'taxRate': taxRate,
      'priceCost': priceCost,
      'specNum': specNum,
      'price': price,
      'highPrice': highPrice,
      'priceMarket': priceMarket,
      'profit': profit,
    };
  }
}
